package com.LeeCode;

import java.util.ArrayList;
import java.util.List;

/**
 * 二叉树的所有路径
 */

public class Code257 {
    public static void main(String[] args) {
        Integer[] arr = {1, 2, 3};
        TreeNode root = Utils.buildTree(arr);
        System.out.println(new Code257().binaryTreePaths(root));
    }

    public List<String> binaryTreePaths(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        List<String> ans = new ArrayList<>();
        dfs(root, sb, ans);
        return ans;
    }

    private void dfs(TreeNode root, StringBuilder sb, List<String> ans) {
        if (root == null) return;
        int len = sb.length();
        if (len != 0) sb.append("->");

        sb.append(root.val);

        if (root.left == null && root.right == null) {
            ans.add(sb.toString());
            sb.setLength(len);
        }else{
            dfs(root.left, sb, ans);
            dfs(root.right, sb, ans);
        }
        sb.setLength(len);
    }
}
